package com.trainingcloud.ctrl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.trainingcloud.mapper.ClassInfoMapper;
import com.trainingcloud.pojo.ClassInfo;
import com.trainingcloud.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 班级管理表 前端控制器
 * </p>
 *
 * @author 纸鹤
 * @since 2022-10-11
 */
@RestController
public class ClassInfoController {
    @Autowired
    private ClassInfoMapper classInfoMapper;



    @RequestMapping("/getTraining")
    public ResultUtil getTraining(int id) {

        List<ClassInfo> classInfos = classInfoMapper.getTraining(id);
        ResultUtil ok = ResultUtil.ok();
        ok.setData(classInfos);
        return ok;
    }

    @RequestMapping("/getClassInfo")
    public ResultUtil getClassInfo(int id) {
        //根据用户ID找到其关联的班级列表
        QueryWrapper<ClassInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("relation_teacher_id", id);
        List<ClassInfo> classInfos = classInfoMapper.selectList(queryWrapper);
        ResultUtil ok = ResultUtil.ok();
        ok.setData(classInfos);
        return ok;
    }

    //    添加班级信息
    @RequestMapping("/addClass")
    public ResultUtil addClass(ClassInfo classInfo) {
        System.out.println(classInfo);
        ResultUtil ok = ResultUtil.ok();
        try {
            classInfoMapper.insert(classInfo);
            ok.setMsg("添加成功");
        } catch (Exception e) {
            return ResultUtil.error("添加失败");
        }
        return ok;
    }

    //    修改班级信息
    @RequestMapping("/editClass")
    public ResultUtil editClass(ClassInfo classInfo) {
        System.out.println(classInfo);
        ResultUtil ok = ResultUtil.ok();
        try {
            classInfoMapper.updateById(classInfo);
            ok.setMsg("修改成功");
        } catch (Exception e) {
            return ResultUtil.error("修改失败");
        }
        return ok;
    }

    //    删除班级信息
    @RequestMapping("/delClass")
    public ResultUtil delClass(int id) {
        ResultUtil ok = ResultUtil.ok();
        try {
            classInfoMapper.deleteById(id);
            ok.setMsg("删除成功");
        } catch (Exception e) {
            return ResultUtil.error("删除失败");
        }
        return ok;
    }
}

